package org.bonorsoft.modules.dingding.controller;

import java.util.Arrays;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.bonorsoft.common.api.vo.Result;
import org.bonorsoft.common.aspect.annotation.PermissionData;
import org.bonorsoft.common.constant.UserConstants;
import org.bonorsoft.common.system.vo.Ztree;
import org.bonorsoft.common.util.oConvertUtils;
import org.bonorsoft.modules.dingding.entity.Params;
import org.bonorsoft.modules.dingding.entity.SysDingUser;
import org.bonorsoft.modules.dingding.service.ISysDingUserService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.bonorsoft.common.aspect.annotation.AutoLog;
import org.bonorsoft.common.system.base.controller.BonorController;

import org.bonorsoft.modules.system.service.ISysConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

/**
 * @Description: 钉钉用户
 * @Author: WilliamYe
 * @Date:   2020-11-13
 * @Version: V1.0
 */
@Slf4j
@Controller
@RequestMapping("/system/user")
public class SysDingUserController extends BonorController<SysDingUser, ISysDingUserService> {
	private String prefix = "system/user";

	@Autowired
	private ISysDingUserService sysUserService;

	@Autowired
	private ISysConfigService configService;

	@RequiresPermissions("system:user:view")
	@GetMapping()
	public String index()
	{
		return prefix + "/user";
	}

	/**
	 * 分页列表查询
	 *
	 * @param sysUser
	 * @param pageNo
	 * @param pageSize
	 * @param params
	 * @return
	 */
	@AutoLog(value = "钉钉用户-分页列表查询")
	@GetMapping(value = "/list")
	@RequiresPermissions("system:user:list")
	@ResponseBody
	@PermissionData(value = "single", userAlias = "u")
	public Result<?> queryPageList(SysDingUser sysUser,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   Params params,
								   HttpServletRequest request) {
		//判断是否开启钉钉用户同步
		String config = configService.selectConfigByKey("sys.user.ding");
		if(config.equals(UserConstants.YES) && isSynchronous(sysUser,params)){
			log.info("开启钉钉同步用户部门信息");
			sysUserService.synchronousUser(sysUser.getDeptIdList());
		}
		Page<SysDingUser> page = new Page<SysDingUser>(pageNo, pageSize);
		IPage<SysDingUser> pageList = sysUserService.selectUser(page, sysUser, params, request);
		return Result.ok(pageList);
	}

	/**
	 * 添加
	 *
	 * @param sysUser
	 * @return
	 */
	@AutoLog(value = "钉钉用户-添加")
	@PostMapping(value = "/add")
	public Result<?> add(@RequestBody SysDingUser sysUser) {
		sysUserService.save(sysUser);
		return Result.ok("添加成功！");
	}

	/**
	 * 编辑
	 *
	 * @param sysUser
	 * @return
	 */
	@AutoLog(value = "钉钉用户-编辑")
	@PutMapping(value = "/edit")
	public Result<?> edit(@RequestBody SysDingUser sysUser) {
		sysUserService.updateById(sysUser);
		return Result.ok("编辑成功!");
	}

	/**
	 * 通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "钉钉用户-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
		sysUserService.removeById(id);
		return Result.ok("删除成功!");
	}

	/**
	 * 批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "钉钉用户-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.sysUserService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.ok("批量删除成功！");
	}

	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "钉钉用户-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
		SysDingUser sysUser = sysUserService.getById(id);
		return Result.ok(sysUser);
	}

	/**
	* 导出excel
	*
	* @param request
	* @param sysUser
	*/
	@RequestMapping(value = "/exportXls")
	public ModelAndView exportXls(HttpServletRequest request, SysDingUser sysUser) {
	  return super.exportXls(request, sysUser, SysDingUser.class, "钉钉用户");
	}

	/**
	* 通过excel导入数据
	*
	* @param request
	* @param response
	* @return
	*/
	@RequestMapping(value = "/importExcel", method = RequestMethod.POST)
	public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
	  return super.importExcel(request, response, SysDingUser.class);
	}

	/**
	 * 判断是否同步
	 * @param sysUser
	 * @param params
	 * @return
	 */
	private boolean isSynchronous(SysDingUser sysUser, Params params) {
		return oConvertUtils.isNotEmpty(sysUser.getDeptIdList())
			&& oConvertUtils.isEmpty(sysUser.getName())
			&& oConvertUtils.isEmpty(sysUser.getMobile())
			&& oConvertUtils.isEmpty(sysUser.getActive())
			&& oConvertUtils.isEmpty(params.getBeginTime())
			&& oConvertUtils.isEmpty(params.getEndTime());
	}

	/**
	 * 加载部门用户树
	 */
	@GetMapping("/treeData")
	@ResponseBody
	@PermissionData(value = "single")
	public List<Ztree> treeData(){
		/*
		redis缓存处理
		 */
		List<Ztree> ztrees = sysUserService.selectDeptUserTree();
		return ztrees;
	}

}
